package com.my.app.tools.util;

import com.my.app.tools.pojo.AppConstants;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppToolUtil {

    /**
     * 对含有中文的字符串进行Unicode编码
     * \ue400 \u9fa5 Unicode表中的汉字的头和尾
     */
    public static String onlyChineseCharEncode(String str) throws Exception {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, URLEncoder.encode(m.group(), AppConstants.ENCODING_UTF8));
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 字节数组转int数组
     *
     * @param byteArray
     * @return
     * @author guochaohui
     * @date 2020-03-27 13:29
     */
    public static int[] byteArrayToIntArray(byte[] byteArray) {
        if (byteArray == null || byteArray.length == 0) {
            return new int[0];
        }
        int[] intArray = new int[byteArray.length];
        for (int i = 0; i < byteArray.length; i++) {
            intArray[i] = byteArray[i];
        }
        return intArray;
    }

    /**
     * int数组转字节数组
     *
     * @param intArray
     * @return
     * @author guochaohui
     * @date 2020-03-27 13:29
     */
    public static byte[] intArrayToByteArray(int[] intArray) {
        if (intArray == null || intArray.length == 0) {
            return new byte[0];
        }
        byte[] byteArray = new byte[intArray.length];
        for (int i = 0; i < intArray.length; i++) {
            byteArray[i] = (byte) intArray[i];
        }
        return byteArray;
    }

    /**
     * 创建矩形坐标
     *
     * @param orgx
     * @param orgy
     * @param endx
     * @param endy
     * @return
     * @author guochaohui
     * @date 2020-04-17 14:03
     */
    public static Rectangle createRectangle(int orgx, int orgy, int endx, int endy) {
        int x = Math.min(orgx, endx);
        int y = Math.min(orgy, endy);
        int width = Math.abs(endx - orgx);
        int height = Math.abs(endy - orgy);
        // 宽高+1，防止0出现
        Rectangle rectangle = new Rectangle(x, y, width, height);
        return rectangle;
    }

    /**
     * 复制图片到系统剪切板
     *
     * @param image
     * @return
     * @author guochaohui
     * @date 2020-04-17 14:04
     */
    public static void copyImageToClipboard(Image image) {
        if (image == null) {
            return;
        }
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable transferable = new ImagesSelection(image);
        clipboard.setContents(transferable, null);
    }

    /**
     * 计算箭头的尾巴坐标
     *
     * @param orgx
     * @param orgy
     * @param endx
     * @param endy
     * @param size
     * @return
     * @author guochaohui
     * @date 2020-04-17 14:04
     */
    public static Point[] calculateArrowOtherPoint(int orgx, int orgy, int endx, int endy, int size) {
        double aDir = Math.atan2(orgx - endx, orgy - endy);
        Point p1 = new Point(endx + xCor(size, aDir + 0.5), endy + yCor(size, aDir + 0.5));
        Point p2 = new Point(endx + xCor(size, aDir - 0.5), endy + yCor(size, aDir - 0.5));
        return new Point[]{p1, p2};
    }

    private static int xCor(int len, double dir) {
        return (int) (len * Math.sin(dir));
    }

    private static int yCor(int len, double dir) {
        return (int) (len * Math.cos(dir));
    }

    /**
     * 计算中间点坐标
     *
     * @param rectangle
     * @return
     * @author guochaohui
     * @date 2020-04-17 14:05
     */
    public static Point calculateMiddlePoint(Rectangle rectangle) {
        return calculateMiddlePoint(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    }

    /**
     * 计算中间点坐标
     *
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     * @author guochaohui
     * @date 2020-04-17 14:05
     */
    public static Point calculateMiddlePoint(int x, int y, int width, int height) {
        return new Point(((x + width) - x) / 2 + x, ((y + height) - y) / 2 + y);
    }

    static class ImagesSelection implements Transferable {
        private Image image;

        public ImagesSelection(Image image) {
            this.image = image;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.imageFlavor};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.imageFlavor.equals(flavor);
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }
    }

}
